"""
Web服务启动入口
"""
import datetime
import uvicorn
import os
from fastapi import FastAPI
from app.api import api_router
from fastapi.staticfiles import StaticFiles
from app.api_test import  test_router
from app.screen import router as screen_router
from app.websocket import  ws_router
from fastapi_utils.tasks import repeat_every
from fastapi_offline import FastAPIOffline

from app.websocket import myjob

def startup(app: FastAPI):
    """
    app 启动事件，定时任务Demo
    :param app:
    :return:
    """
    # def now():
    #     return datetime.datetime.now()
    @repeat_every(seconds=3)
    async def app_start():
        await myjob()

    return app_start

# 容器运行入口
app = FastAPIOffline()
app.add_event_handler("startup", startup(app))
# 获取当前根目录
www_path = os.path.join(os.getcwd(), "www")

# 绑定路由
app.include_router(api_router)
app.include_router(test_router)
app.include_router(screen_router)
app.include_router(ws_router)

# 绑定静态路径
app.mount('/', StaticFiles(directory=www_path), name="www")

# 绑定启动事件
# app.add_event_handler("startup", startup(app))


if __name__ == '__main__':
    # 启动容器
    uvicorn.run(
        'main:app',
        host = '127.0.0.1',
        port = 9909,
        reload = True
    )
